{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import norm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9986501019683699"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "norm.cdf(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import norm\n",
    "\n",
    "\n",
    "def discrete_signal(signal, step_size):\n",
    "    # Discrete signal\n",
    "    disc_sig = (signal / step_size).round() * step_size\n",
    "    disc_sig[disc_sig > 1] = 1\n",
    "    disc_sig[disc_sig < -1] = -1\n",
    "    return disc_sig\n",
    "\n",
    "def get_signal(events, step_size, prob, pred, num_classes, num_threads, **kwargs):\n",
    "    # Get signals from predictions\n",
    "    if prob.shape[0] == 0:\n",
    "        return pd.Series()\n",
    "    # Generate signals from multinomial\n",
    "    signal0 = (prob - 1. / num_classes) / np.sqrt(prob * (1. - prob))\n",
    "    signal0 = pred * (2 * norm.cdf(signal0) - 1)\n",
    "    if 'side' in events:\n",
    "        signal0 *= events.loc[signal0.index, 'side']\n",
    "    # Averaging\n",
    "    df0 = signal0.to_frame('signal').join(events[['t1']], how='left')\n",
    "    df0 = avg_active_signals(df0, num_threads)\n",
    "    signal1 = discrete_signal(signal0=df0, step_size=step_size)\n",
    "    return signal1\n",
    "\n",
    "def mp_avg_active_signals(signals, molecule):\n",
    "    out = pd.Series()\n",
    "    for loc in molecule:\n",
    "        is_act = (signals.index.values <= loc) & ((loc < signal['t1']) | pd.isnull(signals['t1']))\n",
    "        act = signals[is_act].index\n",
    "        if len(act) > 0:\n",
    "            out[loc] = signals.loc[act, 'signal'].mean()\n",
    "        else:\n",
    "            out[loc] = 0\n",
    "    return out\n",
    "\n",
    "def avg_active_signals(signals, num_threads):\n",
    "    # Compute the average signal\n",
    "    # 1) time points where singal changes\n",
    "    t_pnts = set(signals['t1'].dropna().values)\n",
    "    t_pnts = t_pnts.union(signals.index.values)\n",
    "    t_pnts = list(t_pnts)\n",
    "    t_pnts.sort();\n",
    "    out = mp_pandas_obj(mp_avg_active_signals, ('molecule', t_pnts), num_threads, signals=signals)\n",
    "    return out"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 10.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "samples = np.random.uniform(.5, 1., 10000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "zs = (samples - .5) / np.sqrt(samples * (1 - samples))\n",
    "bet_size = 2 * norm.cdf(zs) - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.99884328, 0.51128425, 0.99999709, ..., 0.16633489, 0.46459308,\n",
       "       0.02115305])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bet_size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
